package com.system.manager.modules.assit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.modules.assit.entity.AssExpert;
import com.system.manager.modules.assit.entity.ProjectExpert;
import com.system.manager.modules.assit.mapper.AssExpertMapper;
import com.system.manager.modules.assit.mapper.ProjectExpertMapper;
import com.system.manager.modules.assit.service.IProjectExpertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.projectLibrary.entity.Project;
import com.system.manager.modules.projectLibrary.mapper.ProjectMapper;
import com.system.manager.modules.projectLibrary.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-27
 */
@Service
public class ProjectExpertServiceImpl extends ServiceImpl<ProjectExpertMapper, ProjectExpert> implements IProjectExpertService {

    @Autowired
    private ProjectExpertMapper projectExpertMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private AssExpertMapper assExpertMapper;
    //根据专家id查询所需要管理的项目
    @Override
    public PageDTO<Project> selectByExpertId(Integer page, Integer size, Subject subject) {
        Page<Project> projectPage = new Page<>(page,size);

        //获取用户id
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();

        //根据用户id查询专家id
        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("user_id",userId);
        AssExpert assExpert = assExpertMapper.selectOne(wrapper2);
        Integer expertId = assExpert.getExpertId();


        //获取所有的专家需要管理的项目id
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("expert_id",expertId);
        List<ProjectExpert> list = projectExpertMapper.selectList(wrapper);

        //查询信息
        List<Project> projectList = new ArrayList<>();

        for (ProjectExpert projectExpert : list) {
            Integer projectId = projectExpert.getProjectId();
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("id",projectId);
            Project project = projectMapper.selectOne(wrapper1);
            projectList.add(project);
        }

        PageDTO pageDTO = new PageDTO();
        pageDTO.setData(projectList);
        pageDTO.setCount(list.size());

        return pageDTO;
    }

    /*
    修改状态值
     */

    @Override
    public void updateState(Subject subject,Integer id) {
        //获取用户id
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();

        //根据用户id查询专家id
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        AssExpert assExpert = assExpertMapper.selectOne(wrapper);
        Integer expertId = assExpert.getExpertId();


        //查询该项
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("expert_id",expertId);
        wrapper1.eq("project_id",id);
        ProjectExpert projectExpert1 = projectExpertMapper.selectOne(wrapper1);
        Integer id1 = projectExpert1.getId();

        //封装
        ProjectExpert projectExpert = new ProjectExpert();
        projectExpert.setId(id1);
        projectExpert.setStatus(1);

        projectExpertMapper.updateById(projectExpert);

        //修改完了检查一下这个项目的专家是否全部同意，如果都同意了就修改项目的状态
        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("project_id",id);
        List<ProjectExpert> list = projectExpertMapper.selectList(wrapper2);

        //判断
        for (ProjectExpert expertproject : list) {
            Integer status = expertproject.getStatus();
            if(status == 0){
                return;
            }
        }

        //说明都是同意了，修改项目状态
        //修改项目状态
        Project project = new Project();
        project.setId(id);
        project.setCheckAccept(3);

        projectMapper.updateById(project);

    }
}
